package com.kuyu.wx;

import com.kuyu.msg.MessageKit;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;

@RestController
@Slf4j
public class VerifyController {
    private String TOKEN = "tokenaccesskey";
    /**
     * 服务器验证
     *
     * @param req
     * @param res
     * @throws IOException
     */
    @RequestMapping(value ="/",method= RequestMethod.GET)
    @ResponseBody
    public String verify(HttpServletRequest req, HttpServletResponse res) throws IOException {

        // TODO 检验合法请求

        String echo = req.getParameter("echostr");
        if (echo == null) {
            echo = "chiwanmin";
        }
        return echo;
    }
    //处理公众号传来的消息函数
    @RequestMapping(value ="/",method= RequestMethod.POST)
    public void getInfo(HttpServletRequest req, HttpServletResponse res) {
        //测试接收公众号发过来的消息，封装到MessageKit类中
        /*BufferedReader br=new BufferedReader(new InputStreamReader(req.getInputStream()));
        String str=null;
        while ((str=br.readLine())!=null){
            System.out.println(str.toString());
        }*/
        try {
            Map<String ,String> msgMap= MessageKit.reqMsg2Map(req);
            System.out.println("接收消息="+msgMap);
            String respCon=MessageKit.handlerMeg(msgMap);
            System.out.println("回复消息="+respCon);
            //将回复的内容写到客服端，并设置输出格式和编码
            res.setContentType("application/xml;charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            res.getWriter().write(respCon);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String sort(String token, String timestamp, String nonce) {
        String[] strArray = {token, timestamp, nonce};
        Arrays.sort(strArray);
        StringBuilder sb = new StringBuilder();
        for (String str : strArray) {
            sb.append(str);
        }

        return sb.toString();
    }

    public String sha1(String str) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(str.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
